首张照片快速上传

chengzhenyu 7 years ago
parent
commit
dbbb85267b

+ 2 - 0
app/src/main/java/ai/pai/lensman/bean/PhotoBean.java

@@ -33,6 +33,8 @@ public class PhotoBean implements Serializable {
33 33
 
34 34
     public String lensmanType;
35 35
 
36
+    public int priority;
37
+
36 38
     @Override
37 39
     public boolean equals(Object obj) {
38 40
         if (obj == this) {

+ 9 - 2
app/src/main/java/ai/pai/lensman/db/DBHelper.java

@@ -7,7 +7,7 @@ import android.database.sqlite.SQLiteOpenHelper;
7 7
 public class DBHelper extends SQLiteOpenHelper{
8 8
 
9 9
     private static final String DB_NAME = "paiai";
10
-    private static final int DB_VERSION = 4;
10
+    private static final int DB_VERSION = 5;
11 11
     private static DBHelper instance;
12 12
 
13 13
     public static final String PHOTO_INFO_TABLE = "photo_info_table";
@@ -26,7 +26,7 @@ public class DBHelper extends SQLiteOpenHelper{
26 26
         String GROUP_ID = "group_id";
27 27
         String SESSION_CREATE_TIME = "session_create_time";
28 28
         String LENSMAN_TYPE = "lensman_type";
29
-
29
+        String PRIORITY = "priority";
30 30
     }
31 31
 
32 32
     private DBHelper(Context context){
@@ -54,6 +54,8 @@ public class DBHelper extends SQLiteOpenHelper{
54 54
                 upgradeToVersion3(db);
55 55
             case 3:
56 56
                 upgradeToVersion4(db);
57
+            case 4:
58
+                upgradeToVersion5(db);
57 59
                 break;
58 60
             default:
59 61
                 dropAndRecreateTables(db);
@@ -73,6 +75,10 @@ public class DBHelper extends SQLiteOpenHelper{
73 75
         db.execSQL("Alter table "+ PHOTO_INFO_TABLE+" add column "+PHOTO_INFO_COLUMNS.LENSMAN_TYPE +" VARCHAR");
74 76
     }
75 77
 
78
+    private void upgradeToVersion5(SQLiteDatabase db){
79
+        db.execSQL("Alter table "+ PHOTO_INFO_TABLE+" add column "+PHOTO_INFO_COLUMNS.PRIORITY +" INTEGER");
80
+    }
81
+
76 82
     private void dropAndRecreateTables(SQLiteDatabase db){
77 83
         db.execSQL("DROP TABLE IF EXISTS " + PHOTO_INFO_TABLE);
78 84
         createTables(db);
@@ -94,6 +100,7 @@ public class DBHelper extends SQLiteOpenHelper{
94 100
         sql.append(PHOTO_INFO_COLUMNS.LENSMAN_TYPE).append(" VARCHAR, ");
95 101
         sql.append(PHOTO_INFO_COLUMNS.GROUP_ID).append(" VARCHAR, ");
96 102
         sql.append(PHOTO_INFO_COLUMNS.IS_RAW_PHOTO).append(" INTEGER, ");
103
+        sql.append(PHOTO_INFO_COLUMNS.PRIORITY).append(" INTEGER, ");
97 104
         sql.append(PHOTO_INFO_COLUMNS.SESSION_SEQ).append(" INTEGER, ");
98 105
         sql.append(PHOTO_INFO_COLUMNS.SESSION_DATE).append(" LONG, ");
99 106
         sql.append(PHOTO_INFO_COLUMNS.SESSION_CREATE_TIME).append(" LONG, ");

+ 15 - 2
app/src/main/java/ai/pai/lensman/db/DBService.java

@@ -81,6 +81,7 @@ public class DBService {
81 81
         item.sessionSeq = c.getInt(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.SESSION_SEQ));
82 82
         item.sessionCreateTime = c.getLong(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.SESSION_CREATE_TIME));
83 83
         item.lensmanType = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.LENSMAN_TYPE));
84
+        item.priority = c.getInt(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.PRIORITY));
84 85
         return item;
85 86
     }
86 87
 
@@ -99,6 +100,7 @@ public class DBService {
99 100
         cv.put(DBHelper.PHOTO_INFO_COLUMNS.SESSION_SEQ,item.sessionSeq);
100 101
         cv.put(DBHelper.PHOTO_INFO_COLUMNS.SESSION_CREATE_TIME,item.sessionCreateTime);
101 102
         cv.put(DBHelper.PHOTO_INFO_COLUMNS.LENSMAN_TYPE,item.lensmanType);
103
+        cv.put(DBHelper.PHOTO_INFO_COLUMNS.PRIORITY,item.priority);
102 104
         return cv;
103 105
     }
104 106
 
@@ -232,14 +234,25 @@ public class DBService {
232 234
             try {
233 235
                 db = dbHelper.getReadableDatabase();
234 236
                 db.beginTransaction();
235
-                long time = (System.currentTimeMillis()-30_000);
236 237
                 c = db.rawQuery("select * from " + DBHelper.PHOTO_INFO_TABLE +  " where "
237 238
                         + DBHelper.PHOTO_INFO_COLUMNS.UPLOADED_STATUS + " = '" + 0 + "'"
238
-                        +" and "+DBHelper.PHOTO_INFO_COLUMNS.CAPTURE_TIME+" <" +"'"+time
239
+                        +" and "
240
+                        + DBHelper.PHOTO_INFO_COLUMNS.PRIORITY + " = '" + 1
239 241
                         +"'  order by "
240 242
                         + DBHelper.PHOTO_INFO_COLUMNS.CAPTURE_TIME + " limit 1 ", null);
241 243
                 if (c.moveToNext()) {
244
+                    LogHelper.d("czy","found priority = 1 photo");
242 245
                     photoBean = cursor2PhotoBean(c);
246
+                }else{
247
+                    long time = (System.currentTimeMillis()-Preferences.getInstance().getUploadDelay()*1000);
248
+                    c = db.rawQuery("select * from " + DBHelper.PHOTO_INFO_TABLE +  " where "
249
+                            + DBHelper.PHOTO_INFO_COLUMNS.UPLOADED_STATUS + " = '" + 0 + "'"
250
+                            +" and "+DBHelper.PHOTO_INFO_COLUMNS.CAPTURE_TIME+" <" +"'"+time
251
+                            +"'  order by "
252
+                            + DBHelper.PHOTO_INFO_COLUMNS.CAPTURE_TIME + " limit 1 ", null);
253
+                    if (c.moveToNext()) {
254
+                        photoBean = cursor2PhotoBean(c);
255
+                    }
243 256
                 }
244 257
                 db.setTransactionSuccessful();
245 258
             } catch (Exception e) {

+ 53 - 0
app/src/main/java/ai/pai/lensman/service/PhotoUploadUtils.java

@@ -34,6 +34,9 @@ public class PhotoUploadUtils {
34 34
     public static final int UPLOAD_PHOTO_WIDTH = 1280;
35 35
     public static final int UPLOAD_PHOTO_HEIGHT = 1280;
36 36
 
37
+    public static final int UPLOAD_THUMB_PHOTO_WIDTH = 480;
38
+    public static final int UPLOAD_THUMB_PHOTO_HEIGHT = 480;
39
+
37 40
     public PhotoUploadUtils(String url) throws Exception {
38 41
         this.url = new URL(url);
39 42
     }
@@ -72,6 +75,23 @@ public class PhotoUploadUtils {
72 75
         return out.toByteArray();
73 76
     }
74 77
 
78
+    public byte[] sendThumb() throws Exception {
79
+        initConnection();
80
+        conn.connect();
81
+        ds = new DataOutputStream(conn.getOutputStream());
82
+        writeThumbFileParams();
83
+        writeStringParams();
84
+        paramsEnd();
85
+        InputStream in = conn.getInputStream();
86
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
87
+        int b;
88
+        while ((b = in.read()) != -1) {
89
+            out.write(b);
90
+        }
91
+        conn.disconnect();
92
+        return out.toByteArray();
93
+    }
94
+
75 95
     public byte[] send(boolean isCompress) throws Exception {
76 96
         initConnection();
77 97
         conn.connect();
@@ -118,6 +138,27 @@ public class PhotoUploadUtils {
118 138
             ds.writeBytes(encode(value) + "\r\n");
119 139
         }
120 140
     }
141
+
142
+    //文件数据
143
+    private void writeThumbFileParams() throws Exception {
144
+        Set<String> keySet = fileparams.keySet();
145
+        for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
146
+            String name = it.next();
147
+            File value = fileparams.get(name);
148
+            ds.writeBytes("--" + boundary + "\r\n");
149
+            ds.writeBytes("Content-Disposition: form-data; name=\"" + name
150
+                    + "\"; filename=\"" + encode(value.getName()) + "\"\r\n");
151
+            ds.writeBytes("Content-Type: " + getContentType(value) + "\r\n");
152
+            ds.writeBytes("\r\n");
153
+            byte[] bmp = getThumbBitmapBytes(value);
154
+            ds.write(bmp);
155
+            LogHelper.d("czy","upload final file length="+bmp.length);
156
+            LogHelper.d("czy","Upload bitmap size:"+bmp.length);
157
+            ds.writeBytes("\r\n");
158
+//            saveUploadFile(value,bmp);
159
+        }
160
+    }
161
+
121 162
     //文件数据
122 163
     private void writeFileParams() throws Exception {
123 164
         Set<String> keySet = fileparams.keySet();
@@ -214,6 +255,18 @@ public class PhotoUploadUtils {
214 255
     }
215 256
 
216 257
     //把文件转换成字节数组
258
+    private byte[] getThumbBitmapBytes(File f) throws Exception {
259
+        int uploadWidth = UPLOAD_THUMB_PHOTO_WIDTH;
260
+        int uploadHeight = UPLOAD_THUMB_PHOTO_HEIGHT;
261
+        int uploadQuality = 90;
262
+        LogHelper.d("czy","upload default width = "+uploadWidth+"uploadQuality="+uploadQuality);
263
+        Bitmap bmp = ImageUtils.decodeBitmapWithSize(f.getAbsolutePath(), uploadWidth,uploadHeight);
264
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
265
+        bmp.compress(Bitmap.CompressFormat.JPEG, uploadQuality, baos);
266
+        return baos.toByteArray();
267
+    }
268
+
269
+    //把文件转换成字节数组
217 270
     private byte[] getBytes(File f) throws Exception {
218 271
         FileInputStream in = new FileInputStream(f);
219 272
         ByteArrayOutputStream out = new ByteArrayOutputStream();

+ 5 - 2
app/src/main/java/ai/pai/lensman/service/UploadTask.java

@@ -15,7 +15,7 @@ import ai.pai.lensman.utils.UrlContainer;
15 15
 
16 16
 public class UploadTask extends AsyncTask<Void, Integer, Boolean> {
17 17
 
18
-    private static final String TAG = "UploadTask";
18
+    private static final String TAG = "czy";
19 19
     private PhotoBean bean;
20 20
     private OnPhotoUploadListener listener;
21 21
 
@@ -41,13 +41,16 @@ public class UploadTask extends AsyncTask<Void, Integer, Boolean> {
41 41
                     photoUploadUtils.addTextParameter("photo_id", String.valueOf(bean.photoId));
42 42
                     photoUploadUtils.addTextParameter("nickname",Preferences.getInstance().getUserName());
43 43
                     photoUploadUtils.addTextParameter("lensman_type",bean.lensmanType);
44
-                    String result = new String(photoUploadUtils.send(), "UTF-8");
44
+                    String result = new String(bean.priority == 1 ? photoUploadUtils.sendThumb():photoUploadUtils.send(), "UTF-8");
45 45
                     JSONObject resultObj = new JSONObject(result);
46 46
                     if (resultObj.getInt("status") == 200) {
47 47
                         LogHelper.d(TAG, "上传 UploadTask upload result ok ");
48 48
                         return true;
49
+                    }else {
50
+                        LogHelper.d(TAG, "上传 UploadTask status not 200  "+result);
49 51
                     }
50 52
                 } else {
53
+                    LogHelper.d(TAG, "上传 UploadTask本地文件不存在 ");
51 54
                     return false;
52 55
                 }
53 56
 

+ 1 - 1
app/src/main/java/ai/pai/lensman/session/PhotoRecyclerAdapter.java

@@ -100,7 +100,7 @@ public class PhotoRecyclerAdapter extends RecyclerView.Adapter<PhotoRecyclerAdap
100 100
         holder.photo.setLayoutParams(lp);
101 101
         long timeLeft = delay - (System.currentTimeMillis() - item.captureTime) / 1000;
102 102
 
103
-        if (timeLeft < 0) {
103
+        if (timeLeft < 0 || item.uploadStatus!=PhotoBean.UploadStatus.STATUS_NO_BEGIN) {
104 104
             item.canDelete = false;
105 105
             if (item.uploadStatus == PhotoBean.UploadStatus.STATUS_ERROR) {
106 106
                 holder.uploadStatusText.setText(context.getString(R.string.upload_error) + "," + context.getString(R.string.click_to_retry));

+ 3 - 0
app/src/main/java/ai/pai/lensman/session/SessionPresenter.java

@@ -109,6 +109,9 @@ public class SessionPresenter implements SessionContract.Presenter, SessionInter
109 109
         }
110 110
         LogHelper.d(TAG,"onSessionPhotoCaptured "+bean);
111 111
         bean.groupId = groupId;
112
+        if(photoList.size()==0){
113
+            bean.priority = 1;
114
+        }
112 115
         DBService.getInstance().addPhotoBean(bean);
113 116
         sessionView.showPhotoRecyclerView();
114 117
         sessionView.addNewPhoto(bean);